#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include "Util/MD5.h"

using namespace std;
using namespace toolkit;

// 测试用例结构：输入字符串和预期的 MD5 哈希值
struct TestCase {
    string input;
    string expectedHash;
};

int main() {
    cout << "开始测试 MD5 算法..." << endl;
    
    // 准备测试用例
    vector<TestCase> testCases = {
        {"", "d41d8cd98f00b204e9800998ecf8427e"},
        {"a", "0cc175b9c0f1b6a831c399e269772661"},
        {"abc", "900150983cd24fb0d6963f7d28e17f72"},
        {"message digest", "f96b697d7cb7938d525a2f31aaf161d0"},
        {"abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b"},
        {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f"},
        {"12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"}
    };
    
    // 执行测试
    int passedTests = 0;
    for (const auto& test : testCases) {
        // 计算 MD5 哈希值
        string calculatedHash = md5(test.input);
        
        // 验证结果
        bool passed = (calculatedHash == test.expectedHash);
        if (passed) {
            passedTests++;
            cout << "通过: ";
        } else {
            cout << "失败: ";
        }
        
        cout << "输入=\"" << test.input << "\"" << endl;
        cout << "  预期哈希值: " << test.expectedHash << endl;
        cout << "  计算哈希值: " << calculatedHash << endl;
        
        if (!passed) {
            cout << "  错误: 哈希值不匹配!" << endl;
        }
        cout << endl;
    }
    
    // 输出测试结果摘要
    cout << "测试完成: " << passedTests << " 通过, " 
         << (testCases.size() - passedTests) << " 失败, 共 " 
         << testCases.size() << " 测试用例" << endl;
    
    // 测试 Md5 类的其他功能
    cout << "\n测试 Md5 类的其他功能:" << endl;
    
    // 测试构造函数和 hexDigest 方法
    string testStr = "Hello, World!";
    Md5 md5Instance(testStr);
    cout << "字符串 \"" << testStr << "\" 的 MD5: " << md5Instance.hexDigest() << endl;
    
    // 测试 update 和 finalize 方法
    Md5 md5Incremental;
    md5Incremental.update("Hello", 5);
    md5Incremental.update(", World!", 8);
    md5Incremental.finalize();
    cout << "增量更新的 MD5 (\"Hello\" + \", World!\"): " << md5Incremental.hexDigest() << endl;
    
    // 验证两种方法的结果是否一致
    if (md5Instance.hexDigest() == md5Incremental.hexDigest()) {
        cout << "验证成功: 单次计算和增量更新的结果一致" << endl;
    } else {
        cout << "验证失败: 单次计算和增量更新的结果不一致" << endl;
    }
    
    return 0;
}